package com.lg.mengbase.demo;

import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lg.baselibrary.activity.BaseToolbarActivity;
import com.lg.baselibrary.utils.EditTextClearHandler;
import com.lg.baselibrary.widget.progressbar.circular.CircularProgressButton;
import com.lg.mengbase.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ligang on 2017/6/3.
 * description：
 * 模仿为知笔记中注册登录的效果
 */

public class TestRegistUiActivity extends BaseToolbarActivity {
    @BindView(R.id.btn_regist)
    CircularProgressButton btnRegist;

    @Override
    protected int provideContentViewLayout() {
        return R.layout.activity_test_regist_ui;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        View view = findView(R.id.layout_email);
        View view1 = findView(R.id.layout_password);
        View view2 = findView(R.id.layout_confirm_password);
        new InputHelper(view)
                .setTextView("邮箱")
                .setEditTextHint("邮箱")
                .setEditTextInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        new InputHelper(view1)
                .setTextView("密码")
                .setEditTextHint("密码")
                .setEditTextInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        new InputHelper(view2)
                .setTextView("确认密码")
                .setEditTextHint("确认密码")
                .setEditTextImeOptions(EditorInfo.IME_ACTION_DONE)
                .setEditTextInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        btnRegist.setIndeterminateProgressMode(true);
        btnRegist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btnRegist.getProgress() == 0) {
                    btnRegist.setProgress(50);
                    registAction();
                    Toast.makeText(TestRegistUiActivity.this, "下一次点击将注册成功", Toast.LENGTH_SHORT).show();
                    btnRegist.setClickable(false);
                }
            }
        });
    }
    private boolean success;
    private void registAction(){
        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(!success){
                            btnRegist.setProgress(-1);
                            btnRegist.setProgress(0);
                        }else{
                            btnRegist.setProgress(100);
                           registSuccess();
                        }
                        success = !success;
                        btnRegist.setClickable(true);
                    }
                });
            }
        }.start();
    }
    private void registSuccess(){
        btnRegist.setClickable(false);
        btnRegist.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        },1000);
    }
    @Override
    protected void initData() {

    }

    @Override
    protected void initTitle() {
        setToolbarTitle("为知笔记");
    }


    class InputHelper implements TextWatcher {
        private Animation animation;
        private ViewGroup container;
        private EditText editText;
        private TextView textView;
        private ImageView imageViewClear;

        public InputHelper(View container) {
            this.container = (ViewGroup) container;
            animation = AnimationUtils.loadAnimation(container.getContext(), R.anim.anim_input);
            init();
        }

        public InputHelper setTextView(CharSequence text) {
            textView.setText(text);
            return this;
        }

        public InputHelper setEditTextHint(CharSequence textHint) {
            editText.setHint(textHint);
            return this;
        }

        public InputHelper setEditTextImeOptions(int imeOptions) {
            editText.setImeOptions(imeOptions);
            return this;
        }

        public InputHelper setEditTextInputType(int inputType) {
            editText.setInputType(inputType);
            return this;
        }

        private void init() {
            findView(container);
            new EditTextClearHandler().init(editText, imageViewClear);
            editText.addTextChangedListener(this);
            imageViewClear.setVisibility(View.INVISIBLE);
        }

        private void findView(View view) {
            if (view instanceof ViewGroup) {
                ViewGroup viewGroup = (ViewGroup) view;
                for (int i = 0; i < viewGroup.getChildCount(); i++) {
                    findView(viewGroup.getChildAt(i));
                }
            } else {
                if (view instanceof ImageView) {
                    imageViewClear = (ImageView) view;
                    return;
                }
                if (view instanceof EditText) {
                    editText = (EditText) view;
                    return;
                }
                if (view instanceof TextView) {
                    textView = (TextView) view;
                    return;
                }
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (TextUtils.isEmpty(s)) {
                textView.setVisibility(View.INVISIBLE);
            } else {
                if (textView.getVisibility() != View.VISIBLE) {
                    textView.setVisibility(View.VISIBLE);
                    textView.startAnimation(animation);
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
